package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.dao.UserContactMapper;
import com.xdf.xzymanagementsystem.entity.UserContact;
import com.xdf.xzymanagementsystem.service.SubSystemClient;
import com.xdf.xzymanagementsystem.util.Example;
import com.xdf.xzymanagementsystem.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author chanchaw
 * @create 2022-08-26 14:16
 */
@Service
public class SubSystemClientImpl implements SubSystemClient {
    @Autowired
    private UserContactMapper userContactMapper;

    @Override
    public Boolean isSubUser() {
        Integer userPk = ShiroUtils.getUserPKInteger();
        UserContact userContact = new UserContact();
        userContact.setUser_id(userPk);
        List<UserContact> userContactList = userContactMapper.selectByParam(userContact);
        return userContactList.size() > 0 ? true : false;
    }

    @Override
    public List<UserContact> getSubSystemClients() {
        Integer userPk = ShiroUtils.getUserPKInteger();
        UserContact userContact = new UserContact();
        userContact.setUser_id(userPk);
        userContact.setType(1);// 1表示关联到客户，2表示关联到供应商
        return userContactMapper.selectByParam(userContact);
    }

    @Override
    public <T> List<T> filter8SubSystemUserContact(List<T> records, String methodName, String clientIdFieldName) {
        List<UserContact> clientList = getSubSystemClients();
        if(clientList == null || clientList.size()<=0) return records;// 不是子系统用户则返回所有数据
        List<T> ret = records.stream().filter(item -> {

            try {
                Method getMethod = item.getClass().getMethod(methodName,Class.forName("java.lang.Object"));
                Integer clientId = (Integer)getMethod.invoke(item, clientIdFieldName);
                List<UserContact> targetClients = clientList.stream().filter(item1 -> Integer.parseInt(item1.getContact_code()) == clientId).collect(Collectors.toList());
                return targetClients.size() > 0 ? true : false;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } finally {
            }

        }).collect(Collectors.toList());

        return ret;
    }

    @Override
    public <T> List<T> filter8SubSystemUserContact(List<T> records, String methodName) {
        List<UserContact> clientList = getSubSystemClients();
        if(clientList == null || clientList.size()<=0) return records;// 不是子系统用户则返回所有数据
        List<T> ret = records.stream().filter(item -> {

            try {
                Method getMethod = item.getClass().getMethod(methodName);// 没有参数的方法
                Integer clientId = (Integer) Optional.ofNullable(getMethod.invoke(item)).orElse(0);
                List<UserContact> targetClients = clientList.stream().filter(item1 -> Integer.parseInt(item1.getContact_code()) == clientId).collect(Collectors.toList());
                return targetClients.size() > 0 ? true : false;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } finally {

            }

        }).collect(Collectors.toList());

        return ret;
    }

    @Override
    public <T> T filter8SubSystemUserContact(T record, String methodName) {
        List<UserContact> clientList = getSubSystemClients();
        if(clientList == null || clientList.size()<=0) return record;// 不是子系统用户则返回所有数据

        try {
            Method getMethod = record.getClass().getMethod(methodName);// 没有参数的方法
            Integer clientId = (Integer)getMethod.invoke(record);
            List<UserContact> targetClients = clientList.stream().filter(item1 -> Integer.parseInt(item1.getContact_code()) == clientId).collect(Collectors.toList());
            return targetClients.size() > 0 ? record : null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } finally {

        }
    }

    @Override
    public Example addInCondition(Example example,String clientIdFieldName) {
        List<UserContact> clientList = getSubSystemClients();
        if(clientList == null || clientList.size()<=0) return example;// 不是子系统用户则返回所有数据

//        String[] clientIds = clientList.stream().map(item -> item.getContact_code()).toArray(String[]::new);
        Integer[] clientIds = clientList.stream().map(item -> new Integer(item.getContact_code())).toArray(Integer[]::new);
        example.andIn(clientIdFieldName,clientIds);
        return example;
    }


}
